#include "stdafx.h"

#include "IAlphaMulBtn.h"
#include "Control/Container.h"


#define MAKE_RECT( X,Y,W,H )  X,Y,(X+W),(Y+H)


CIAlphaMulBtn::CIAlphaMulBtn(void)
	:CBaseICtrl()
{
	Init();
}

CIAlphaMulBtn::CIAlphaMulBtn(UINT nID)
	:CBaseICtrl( nID )
{
	Init();
}

CIAlphaMulBtn::CIAlphaMulBtn(wstring strCaption,UINT nID)
	:CBaseICtrl( strCaption,nID )
{
	Init();
}

CIAlphaMulBtn::CIAlphaMulBtn(DWORD dwStyle,wstring strCaption, const CRect &rc, UINT nID )
	:CBaseICtrl( dwStyle, strCaption, rc,nID )
{
	Init();
}

CIAlphaMulBtn::CIAlphaMulBtn(DWORD dwStyle, wstring strCaption, int x, int y, int cx, int cy, UINT nID )
	:CBaseICtrl( dwStyle, strCaption, x, y, cx, cy,  nID )
{
	Init();
}

CIAlphaMulBtn::~CIAlphaMulBtn(void)
{
	ReleaseMulResState( );
}

void	CIAlphaMulBtn::ReleaseMulResState( )
{
	for ( int i=0;i<m_VecMulResBtn.size();i++ )
	{
		if( NULL!=m_VecMulResBtn[i] )
		{
			delete m_VecMulResBtn[i];
			m_VecMulResBtn[i] = NULL;
		}
	}
	m_VecMulResBtn.clear();
	m_VecMulRect.clear();
}

void CIAlphaMulBtn::Init()
{
	m_nBtnState = 0;

	//m_rcState[ST_TOP_NOR].SetRect( MAKE_RECT (20,36,75,75) );
	//m_rcState[ST_ACTIVE].SetRect( MAKE_RECT (30,71,114,114) );
	//m_rcState[ST_NORMAL].SetRect( MAKE_RECT (34,72,110,110) );
	//m_rcState[ST_BOTTOM_NOR].SetRect( MAKE_RECT (20,152,75,75) );

	//m_rcState[ST_MID_UP].SetRect( MAKE_RECT (36,60,90,92) );
	//m_rcState[ST_MID_DN].SetRect( MAKE_RECT (36,102,90,92) );

	//m_rcState[ST_TOP_MOV].SetRect( MAKE_RECT (14,28,60,60) );
	//m_rcState[ST_BOTTOM_MOV].SetRect( MAKE_RECT (14,168,60,60) );

}

void  CIAlphaMulBtn::ReInitBtnState( int StateCount )
{
	ReleaseMulResState( );

	//vector<int>().swap(nums); 
	vector<CFAlphaRes*>().swap( m_VecMulResBtn );
	vector<CRect>().swap( m_VecMulRect );

	m_VecMulResBtn.resize( StateCount );
	m_VecMulRect.resize( StateCount );

	for ( int i=0;i<StateCount;i++ )
	{
		m_VecMulResBtn[i] = NULL;
		m_VecMulRect[i].SetRectEmpty();
	}

	//release vector unused memory
	//{ 
	//	vector<CFAlphaRes*> tmp = m_VecMulResBtn;   
	//	m_VecMulResBtn.swap(tmp); 
	//}      

}

void	CIAlphaMulBtn::SetStateRect( int nState,LPRECT pRC )
{
	if ( pRC!=NULL&& nState>=0 &&nState<m_VecMulRect.size() )
	{
		m_VecMulRect[nState].CopyRect( pRC );
	}
}

// conctrol only have one image
BOOL	CIAlphaMulBtn::LoadRes( wstring strRes,CMyUnCompress* pPack )
{
	return LoadStateRes(  strRes,0,pPack );
}

//if contrl only have one img,this function just like LoadRes,here the same
BOOL	CIAlphaMulBtn::LoadStateRes( wstring strRes,int nState,CMyUnCompress* pPack )
{
	BOOL br = FALSE;
	if ( nState<m_VecMulResBtn.size() && nState>=0 )
	{
		if ( m_VecMulResBtn[nState]==NULL )	
			m_VecMulResBtn[nState] = new CFAlphaRes();
		br = m_VecMulResBtn[nState]->LoadResData( strRes,pPack );
	}

	return br;
}

BOOL	CIAlphaMulBtn::InitStateResRect( wstring strRes,int nState,LPRECT pRect, CMyUnCompress* pPack )
{
	SetStateRect( nState,pRect );
	return LoadStateRes( strRes, nState, pPack );
}

BOOL	CIAlphaMulBtn::CloneStateBtn( int nSrcSt,int dst_State,LPRECT prcDst )
{
	if ( nSrcSt>=m_VecMulResBtn.size() || nSrcSt<0 && dst_State>=m_VecMulResBtn.size() && dst_State<0 )
		return FALSE;
	if ( m_VecMulResBtn[nSrcSt]==NULL )
		return FALSE;

	if( NULL!=m_VecMulResBtn[dst_State] )
	{
		delete m_VecMulResBtn[dst_State];
		m_VecMulResBtn[dst_State] = NULL;
	}
	m_VecMulResBtn[dst_State] = new CFAlphaRes( *(m_VecMulResBtn[nSrcSt]) );
	if ( prcDst!=NULL )
		m_VecMulRect[dst_State].CopyRect(prcDst);

	return TRUE;
}

void	CIAlphaMulBtn::SetBtnCurState( int nState )
{
	if ( nState<m_VecMulResBtn.size() && nState>=0 )
		m_nBtnState = nState;
}

void  CIAlphaMulBtn::Paint(CDC* pDC)
{
	return;
}

void  CIAlphaMulBtn::ImprovePaint( HANDLE hDB ,HDC hdc )
{
	//if ( ST_UNKNOW==m_nBtnState )return ;

	if ( m_nBtnState<m_VecMulResBtn.size() && m_nBtnState>=0 )
	{
		if ( m_VecMulResBtn[m_nBtnState]!=NULL && !m_VecMulRect[m_nBtnState].IsRectEmpty() )
		{
			FASTBLTALPHABPP( m_VecMulResBtn[m_nBtnState], hDB,m_VecMulRect[m_nBtnState].TopLeft(),GetConBKSize() );
		}
	}

	if ( hdc!=NULL )
	{
		PaintCaption( hdc );
	}
}

CRect	CIAlphaMulBtn::GetCurrentRect()
{
	if ( m_nBtnState<m_VecMulRect.size() && m_nBtnState>=0 )
		return m_VecMulRect[m_nBtnState];

	return CRect(0,0,0,0);

}

void  CIAlphaMulBtn::ImproveStatePaint( HANDLE hDB ,int state )
{
	if ( state<m_VecMulResBtn.size() && state>=0 )
	{
		if ( m_VecMulResBtn[state]!=NULL && !m_VecMulRect[state].IsRectEmpty() )
		{
			FASTBLTALPHABPP( m_VecMulResBtn[state],  hDB,m_VecMulRect[state].TopLeft(),GetConBKSize() );
		}
	}
}

void  CIAlphaMulBtn::PaintCaption( HDC hdcTxt )
{
	SetBkMode( hdcTxt, TRANSPARENT );
	if ( GetState()==STATE_PUSHED )
	{
		SetTextColor( hdcTxt,m_clrTxtActive );
	}
	else
	{
		SetTextColor( hdcTxt,m_clrTxtNormal );
	}

	CRect rcTxt( m_rcControl );
	rcTxt.left += m_szTextOffset.cx;
	rcTxt.top += m_szTextOffset.cy;

	HFONT hOldFont = NULL;
	if ( m_pFont!=NULL )
		hOldFont = (HFONT)SelectObject( hdcTxt, m_pFont->m_hFont );
	
	DrawText( hdcTxt, m_strCaption.c_str(),m_strCaption.length(),rcTxt,DT_CENTER );
	
	if ( hOldFont!=NULL )
		SelectObject( hdcTxt,hOldFont );
	
}

void  CIAlphaMulBtn::Click(DWORD dwClick, POINT pt)
{
	if ( !IsEnabled() || !IsVisible() || m_rcControl.IsRectEmpty() )
	{
		return;
	}

	if ( dwClick&CLICK_DOWN )
	{
		SetState( STATE_PUSHED ); 
	}
	else if ( dwClick&CLICK_UP )
	{
		SetState( STATE_NORMAL ); 
	}

	if ( m_pContainer!=NULL && IsWindow( m_pContainer->m_hWnd) )
	{
		//CClientDC ClientDC( m_pContainer->m_hWnd );
		//CContainer *pImpCon = (CImproveCon *)m_pContainer;
		//pImpCon->Paint( &ClientDC,m_rcControl );
	}

	if ( dwClick&CLICK_UP && GetRect().PtInRect( pt ) )
	{
		if ( m_pContainer!=NULL && m_pContainer->m_hWnd!=NULL )
		{
			CUtil::PlayBeepSound();
			::SendMessage( m_pContainer->m_hWnd,WM_COMMAND,GetID(),NULL);
		}
	}
}

